const app = getApp();
var base64  = require('../../../utils/base64');
Page({
  data: {
		disabled:false,
		editCard:false,
		onDate:false,
		minDate: new Date().getTime(),
    maxDate: new Date(2030,1,1).getTime(),
    currentDate: new Date().getTime(),
    card:{
			is_lock:0,
			price:0,
			num:0,
			name:'',
			locktime:'',
			content:'',
			coupon:{
				howmuch:0,
				amount:0,
				name:'',
				tips:''
			},
		},
	},
	//生命周期函数--监听页面加载
	onLoad: function (options){
		app.getParam(options,(param) => {
			this.data.id = app.util().isNull(param.id) ? 0 : param.id
			this.getCard()
		})
	},
	//读取优惠券
	getCard: function () {
    app.api().Get('api/v1/ais/gmcard/storeCardInfo',{id:this.data.id},(result) => {
			if(200 == result.code){
				this.setData({
					card:result.data,
					editCoupon:true,
				});
				this.onEditorReady()
			}
    })
	},
	//删除优惠券
	onDelete: function () {
    app.api().Get('api/v1/ais/gmcard/storeCardDel',{id:this.data.id},(result) => {
			app.wxAlert(result.msg,()=> {
				wx.navigateBack({
					delta: 1
				})
			})   
    })
	},
	/**
	 * 创建会员卡
	 */
  formSubmit: function (e) {
		var data = e.detail.value;
    if (app.util().isNull(data.name)) {
			app.wxAlert('储值活动名称必须填写');return;
    }else if (app.util().isNull(data.locktime)) {
			app.wxAlert('结束日期必须填写');return;
    }else if (app.util().isNull(data.price)) {
			app.wxAlert('付款金额必须填写');return;
    }else if (app.util().isNull(data.num)) {
			app.wxAlert('发现数量必须填写');return;
    } else if (app.util().isNull(data.amount)) {
			app.wxAlert('储值额度必须输入');return;
    } else if (app.util().isNull(data.howmuch)) {
			app.wxAlert('使用门槛必须填写');return;
    } else if (app.util().isNull(data.tips)) {
			app.wxAlert('规则简述必须填写');return;
		}else{
			this.editorCtx.getContents({success:(res)=>{
				if (app.util().isNull(res.text)) {
					app.wxAlert('储值须知必须填写');return;
				}else{
					this.setData({disabled:true})
					app.api().Post('api/v1/ais/gmcard/edit',{
						id:this.data.id,
						name: data.name,
						price: data.price,
						amount: data.amount,
						howmuch: data.howmuch,
						num: data.num,
						locktime: data.locktime,
						tips: data.tips,
						content: base64.encode(res.html),
					},(rel) => {
						this.setData({disabled:false})
						app.wxAlert(rel.msg,()=> {
							wx.navigateBack({
								delta: 1
							})
						})
					})
				}
			}})
		}
	},
	//保存表单值
	onInput: function (e) {
		var card = this.data.card;
		card[e.target.dataset.name] = app.util().isNull(e.detail) ? 0 : e.detail;
		this.setData({
			card: card
		})
	},
	onInputCoupon: function (e) {
		var name = e.target.dataset.name;
		var card = this.data.card;
		card.coupon[name] = app.util().isNull(e.detail) ? 0 : e.detail;
		this.setData({
			card: card
		})
	},
	//时间选择器
	selectDate: function (e) {
		this.setData({
			onDate:!this.data.onDate
		});
	},
	onDateConfirm: function (event) {
		var card = this.data.card;
				card.locktime = app.util().dateFormat(event.detail,'Y/M/D')
		this.setData({
			card:card
		});
		this.selectDate();
	},
	//编辑器
	onEditorReady() {
    wx.createSelectorQuery().select('#editor').context((res) => {
			this.editorCtx = res.context
			setTimeout(()=>{
				this.editorCtx.setContents({
					html:this.data.card.content
				})
			},1000)
    }).exec()
	},
	onStatusChange(e) {
		const formats = e.detail
    this.setData({ formats })
	},
	format(e) {
    let {name,value} = e.target.dataset
    if (!name) return
    this.editorCtx.format(name, value)
	},
	remove(e) {
		this.editorCtx.removeFormat()
	},
})